#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2016-04-01 16:19:03
# @Author  : Gtlions Lai (gtlions.l@qq.com)
# @Link    : http://blog.csdn.net/gtlions
# @Version : 1.0.0
"""summarization ahout this script.
detail ahout this script
    Class(): summarization about Class
    ...
    function(): summarization about function
    ...
"""
import paramiko
import threading


def ssh2(ip, username, passwd, cmd):
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ip, 22, username, passwd, timeout=5)
        print "-----------------------execute start-----------------------"
        for m in cmd:
            stdin, stdout, stderr = ssh.exec_command(m)
           # stdin.write("Y")   #简单交互，输入 ‘Y’
            out = stdout.readlines()
            # 屏幕输出
            for o in out:
                print o,
        print "-----------------------execute end-------------------------"
        print '%s\tOK\n' % (ip)
        ssh.close()
    except:
        print '%s\tError\n' % (ip)
if __name__ == '__main__':
    cmd = ["hostname", "cat /etc/passwd|egrep 'lp|uucp|nobody|games|rpm|smmsp|nfsnobody'"]  # 你要执行的命令列表
    username = "app"  # 用户名
    passwd = "nlsofT1!"  # 密码
    threads = []  # 多线程
    print "Begin......"
    for i in range(178, 187):
        ip = '211.143.146.' + str(i)
        # for threads do
        #threads_do = threading.Thread(target=ssh2, args=(ip, username, passwd, cmd))
        #threads_do.start()
        # for single threads do
        ssh2(ip, username, passwd, cmd)